package com.kangyi.admin.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.FourHighMapper;
import com.kangyi.admin.service.GuestsStayingService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.GuestsStayingDTO;
import com.kangyi.dto.GuestsStayingPageQueryDTO;
import com.kangyi.dto.UpdateGuestsStayingDTO;
import com.kangyi.entity.GuestsStaying;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.NursingHomeException;
import com.kangyi.admin.mapper.GuestsStayingMapper;
import com.kangyi.admin.mapper.HealthInfoMapper;
import com.kangyi.exception.PermissionsException;
import com.kangyi.result.PageResult;
import com.kangyi.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class GuestsStayingServiceImpl implements GuestsStayingService {

    @Autowired
    private GuestsStayingMapper guestsStayingMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private HealthInfoMapper healthInfoMapper;

    @Autowired
    private FourHighMapper fourHighMapper;

    // 调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        // 判断权限是否既不是1也不是2，如果是则抛出异常
        if (!adminUserVO.getPermissions().equals(1) && !adminUserVO.getPermissions().equals(2) && !adminUserVO.getPermissions().equals(3)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public PageResult pageQuery(GuestsStayingPageQueryDTO guestsStayingPageQueryDTO) {
        validateUserPermissions();
        PageHelper.startPage(guestsStayingPageQueryDTO.getPage(),guestsStayingPageQueryDTO.getPageSize());
        Page<GuestsStayingPageQueryVO> page = guestsStayingMapper.pageQuery(guestsStayingPageQueryDTO);
        long total = page.getTotal();
        List<GuestsStayingPageQueryVO> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void deleteGuests(List<Integer> ids) {
        validateUserPermissions();
        guestsStayingMapper.deleteGuests(ids);
    }

    @Override
    public GuestsStayingDetailVO detailGuest(Integer id) {
        validateUserPermissions();
        GuestsStayingDetailVO guestsStayingDetailVO = guestsStayingMapper.detailGuest(id);
        if (guestsStayingDetailVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        HealthInfoVO healthInfoVO = healthInfoMapper.getById(id);

        guestsStayingDetailVO.setHealthInfoVO(healthInfoVO);

        FourHighVO fourHighVO = fourHighMapper.getById(id);

        guestsStayingDetailVO.setFourHighVO(fourHighVO);

        return guestsStayingDetailVO;
    }

    @Override
    public void editGuest(GuestsStayingDTO guestsStayingDTO) {
        validateUserPermissions();
        GuestsStaying guestsStaying = new GuestsStaying();
        BeanUtil.copyProperties(guestsStayingDTO,guestsStaying);
        guestsStayingMapper.editGuest(guestsStaying);
    }


}
